#include "TooltipManager.h"
#include <QDebug>
TooltipManager::TooltipManager(QObject *parent)
    : QObject(parent), m_label(nullptr), m_isDisplaying(false)
{
    m_hideTimer.setSingleShot(true);
    connect(&m_hideTimer, &QTimer::timeout, this, &TooltipManager::onTimeout);
}

TooltipManager::~TooltipManager()
{
}

void TooltipManager::bindLabel(QLabel *label)
{
    m_label = label;
}

void TooltipManager::setTooltip(const QString &text, int durationMs)
{
    if (!m_label) {
        qWarning() << "QLabel is not bound!";
        return;
    }

//    if (m_isDisplaying) {
//        // If currently displaying, queue the new tooltip
//        if (m_tooltipQueue.size() >= MAX_QUEUE_SIZE) {
//            m_tooltipQueue.dequeue(); // Remove oldest
//        }
//        m_tooltipQueue.enqueue(qMakePair(qMakePair(text, QDateTime::currentDateTime()), durationMs));
//        return;
//    }

    m_label->setText(text);
    m_label->setVisible(true);
    m_hideTimer.start(durationMs);
    m_isDisplaying = true;
}

void TooltipManager::onTimeout()
{
    m_label->setVisible(false);
    m_isDisplaying = false;

    if (!m_tooltipQueue.isEmpty()) {
        processNextTooltip();
    }
}

void TooltipManager::processNextTooltip()
{
    if (m_tooltipQueue.isEmpty()) {
        return;
    }

    auto tooltip = m_tooltipQueue.dequeue();
    const QString &text = tooltip.first.first;
    int durationMs = tooltip.second;

    m_label->setText(text);
    m_label->setVisible(true);
    m_hideTimer.start(durationMs);
    m_isDisplaying = true;
}
